package lu.med.praxiswahl.wahlumed.client.lieferanten;

import java.awt.BorderLayout;
import java.awt.GridLayout;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import lu.med.praxiswahl.wahlumed.client.utilities.UniCode;
import lu.med.praxiswahl.wahlumed.client.wclient.Main;


/**
 * 
 * @author Lars Holger Roth
 */
@SuppressWarnings("serial")
public class LieferantenNewDatasetDialog extends JDialog
{
    private JTextField[] tf;
    private JCheckBox cb;

    public LieferantenNewDatasetDialog(Main owner, LieferantenTableModel model)
    {
	super(owner, "Neuen Artikel hinzuf" + UniCode.getue() + "gen", true);

	this.setLayout(new BorderLayout());

	JPanel left, right, bottom;

	left = new JPanel(new GridLayout(0, 2));
	right = new JPanel(new GridLayout(0, 2));

	tf = new JTextField[15];
	for (int i = 0; i < 15; i++)
	{
	    switch (i)
	    {
	    case 0:
		left.add(new JLabel("Artikel:"));
		break;
	    case 1:
		left.add(new JLabel("Lieferant:"));
		break;
	    case 2:
		left.add(new JLabel("Tel.:"));
		break;
	    case 3:
		left.add(new JLabel("Fax.:"));
		break;
	    case 4:
		left.add(new JLabel("El.Adresse:"));
		break;
	    case 5:
		left.add(new JLabel("Bestelldatum:"));
		break;
	    case 6:
		left.add(new JLabel("Eingangsdatum:"));
		break;
	    case 7:
		right.add(new JLabel("Preis:"));
		break;
	    case 8:
		right.add(new JLabel("Kategorie:"));
		break;
	    case 9:
		right.add(new JLabel("Bestellmenge:"));
		break;
	    case 10:
		right.add(new JLabel("Bestand:"));
		break;
	    case 11:
		right.add(new JLabel("Rechnungseingang:"));
		break;
	    case 12:
		right.add(new JLabel("Bezahlt am:"));
		break;
	    case 13:
		right.add(new JLabel("Grenzwert:"));
		break;
	    case 14:
		right.add(new JLabel("TVA:"));
	    }
	    tf[i] = new JTextField();
	    if (i < 7)
	    {
		left.add(tf[i]);
	    }
	    else
		right.add(tf[i]);
	}

	bottom = new JPanel();
	JButton okButton = new JButton("Ok");
	okButton
		.addActionListener(new LieferantenNewDatasetButtonActionListener(
			this, model, owner));
	JButton cancelButton = new JButton("Abbrechen");
	cancelButton
		.addActionListener(new LieferantenCancelActionListener(this));
	bottom.add(okButton);
	bottom.add(cancelButton);

	this.add(bottom, BorderLayout.SOUTH);
	this.add(left, BorderLayout.WEST);
	this.add(right, BorderLayout.EAST);

	this.pack();
	this.setVisible(true);
    }

    public LieferantenNewDatasetDialog(Main owner, LieferantenTableModel model,
	    int index)
    {
	super(owner, "Neuen Artikel hinzuf" + UniCode.getue() + "gen", true);

	this.setLayout(new BorderLayout());

	JPanel left, right, bottom;

	left = new JPanel(new GridLayout(0, 2));
	right = new JPanel(new GridLayout(0, 2));

	tf = new JTextField[15];
	for (int i = 0; i < 15; i++)
	{
	    switch (i)
	    {
	    case 0:
		left.add(new JLabel("Artikel:"));
		break;
	    case 1:
		left.add(new JLabel("Lieferant:"));
		break;
	    case 2:
		left.add(new JLabel("Tel.:"));
		break;
	    case 3:
		left.add(new JLabel("Fax.:"));
		break;
	    case 4:
		left.add(new JLabel("E-Adresse:"));
		break;
	    case 5:
		left.add(new JLabel("Bestelldatum:"));
		break;
	    case 6:
		left.add(new JLabel("Lieferdatum:"));
		break;
	    case 7:
		right.add(new JLabel("Nettopreis:"));
		break;
	    case 8:
		right.add(new JLabel("Kategorie:"));
		break;
	    case 9:
		right.add(new JLabel("Bestellmenge:"));
		break;
	    case 10:
		right.add(new JLabel("Bestand:"));
		break;
	    case 11:
		right.add(new JLabel("Rechnungseingang:"));
		break;
	    case 12:
		right.add(new JLabel("Bezahlt am:"));
		break;
	    case 13:
		right.add(new JLabel("Grenzwert:"));
		break;
	    case 14:
		right.add(new JLabel("TVA:"));
	    }
	    tf[i] = new JTextField();
	    if (i < 7)
	    {
		left.add(tf[i]);
	    }
	    else
	    {
		right.add(tf[i]);
	    }
	}

	bottom = new JPanel();
	JButton okButton = new JButton("Ok");
	okButton
		.addActionListener(new LieferantenNewDatasetButtonActionListener(
			this, model, owner));
	JButton cancelButton = new JButton("Abbrechen");
	cancelButton
		.addActionListener(new LieferantenCancelActionListener(this));
	bottom.add(okButton);
	bottom.add(cancelButton);

	this.add(bottom, BorderLayout.SOUTH);
	this.add(left, BorderLayout.WEST);
	this.add(right, BorderLayout.EAST);

	if (index >= 0)
	{
	    // Tr�gt die Werte des entsprechenden Datensatzes ein
	    LieferantenTableDataset ds = model.getRow(index);
	    tf[0].setText(ds.getArtikel());
	    tf[1].setText(ds.getLieferant());
	    tf[2].setText(ds.getTel());
	    tf[3].setText(ds.getFax());
	    tf[4].setText(ds.getEadresse());
	    tf[5].setText(ds.getBestelldatum());
	    tf[6].setText(ds.getEingangsdatum());
	    tf[7].setText(ds.getPreis());
	    tf[9].setText(ds.getBestellmenge());
	    tf[10].setText(ds.getBestand());
	    tf[11].setText(ds.getRechnungseingang());
	    tf[12].setText(ds.getBezahltam());
	    tf[13].setText("" + ds.getPercentage());
	    tf[14].setText(ds.getTva());
	}

	this.pack();
	this.setVisible(true);
    }

    public LieferantenTableDataset getDataset()
    {
	LieferantenTableDataset ds;
	ds = new LieferantenTableDataset();
	ds.setArtikel(tf[0].getText());
	ds.setLieferant(tf[1].getText());
	ds.setTel(tf[2].getText());
	ds.setFax(tf[3].getText());
	ds.setEadresse(tf[4].getText());
	ds.setBestelldatum(tf[5].getText());
	ds.setEingangsdatum(tf[6].getText());
	ds.setPreis(tf[7].getText());
	ds.setBestellmenge(tf[9].getText());
	ds.setBestand(tf[10].getText());
	ds.setRechnungseingang(tf[11].getText());
	ds.setBezahltam(tf[12].getText());
	ds.setTva(tf[14].getText());
	try
	{
	    ds.setPercentage(Integer.parseInt(tf[13].getText()));
	}
	catch (NumberFormatException e)
	{
	    ds.setPercentage(0);
	}
	return ds;
    }

}
